package cool.leeson.library.controller.library;

import com.alibaba.fastjson.JSONObject;
import cool.leeson.library.service.library.RulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("rules")
public class RulesController {

    @Autowired
    private RulesService rulesService;

    // 获取规则
    @GetMapping
    public ResponseEntity<?> getRules() {
        Map<String, Object> res = new HashMap<>();
        String rules = rulesService.getRules();
        res.put("code", 200);
        res.put("data", rules);
        return ResponseEntity.ok(res);
    }

    // 更新规则
    @PostMapping("update")
    public String updateRules(@RequestBody String newRules) {
        JSONObject jsonObject = (JSONObject) JSONObject.parse(newRules);
        // 将新规则包装成 JSON 格式

        System.out.println(jsonObject.get("data"));
        rulesService.updateRules((String) jsonObject.get("data"));
        return "规则更新成功！";
    }
}
